package com.simon.hse.activity.safe.workpermit;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.simon.hse.MyApplication;
import com.simon.hse.R;
import com.simon.hse.activity.BaseActivity;
import com.simon.hse.adapter.CommonEmptyAdapter;
import com.simon.hse.adapter.CommonViewHolder;
import com.simon.hse.http.CommonCallBack;
import com.simon.hse.http.HttpManager;
import com.simon.hse.http.entity.LicenseGetListInput;
import com.simon.hse.http.entity.LicenseGetListOutput;
import com.simon.hse.utils.ToastUtils;
import com.simon.hse.view.RotateTextView;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;

/**
 * 法律法规管理
 */
@SuppressWarnings("all")
@ContentView(R.layout.activity_work_permit_query)
public class WorkPermitQueryListActivity extends BaseActivity {
    @ViewInject(R.id.tv_common_title)
    TextView mTv_common_title;

    @ViewInject(R.id.iv_common_right)
    ImageView mIv_common_right;

    @ViewInject(R.id.tv_function_1)
    TextView mTv_function_1;
    @ViewInject(R.id.ll_function_1)
    LinearLayout mLl_function_1;

    @ViewInject(R.id.tv_function_2)
    TextView mTv_function_2;
    @ViewInject(R.id.ll_function_2)
    LinearLayout mLl_function_2;

    @ViewInject(R.id.tv_function_3)
    TextView mTv_function_3;
    @ViewInject(R.id.ll_function_3)
    LinearLayout mLl_function_3;

    @ViewInject(R.id.tv_function_4)
    TextView mTv_function_4;
    @ViewInject(R.id.ll_function_4)
    LinearLayout mLl_function_4;

    @ViewInject(R.id.common_swiperefreshlayout)
    SmartRefreshLayout mSmartRefresh;

    @ViewInject(R.id.common_recyclerview)
    RecyclerView mRecyclerView;

    private CommonEmptyAdapter mCommonEmptyAdapter;
    private int currentPage = 1, totalPage = 1;
    private List<LicenseGetListOutput.DataBean.PageDataBean> mDataBeanList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mTv_common_title.setText("作业许可证查询");
        initRecyclerView();
        initData();
    }

    @Event(value = {R.id.iv_common_left, R.id.iv_common_right, R.id.ll_function_1, R.id.ll_function_2, R.id.ll_function_3, R.id.ll_function_4})
    private void clickButton(View view) {
        switch (view.getId()) {
            case R.id.iv_common_left:
                finish();
                break;
            case R.id.iv_common_right:
                Intent intent = new Intent(WorkPermitQueryListActivity.this, WorkPermitApplyActivity.class);
                startActivity(intent);
                break;
            case R.id.ll_function_1:
                initSafePop(mLl_function_1, mTv_function_1, MyApplication.getInstance().mSafeQureyStateList);
                break;
            case R.id.ll_function_2:
                initSafePop(mLl_function_2, mTv_function_2, MyApplication.getInstance().mSafeWorkAreaList);
                break;
            case R.id.ll_function_3:
                publishSafeDate(mTv_function_3);
                break;
            case R.id.ll_function_4:
                publishSafeDate(mTv_function_4);
                break;
        }
    }

    private void initRecyclerView() {
        mTv_function_1.setText("状态");
        mTv_function_2.setText("工作区域");
        mTv_function_3.setText("申请时间");
        mTv_function_4.setText("有效时间");
        mSmartRefresh.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                if (currentPage * 10 < totalPage) {
                    currentPage++;
                    initData();
                } else {
                    mSmartRefresh.finishLoadMoreWithNoMoreData();
                }
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                currentPage = 1;
                mDataBeanList.clear();
                initData();
            }
        });

        LinearLayoutManager llm = new LinearLayoutManager(WorkPermitQueryListActivity.this);
        mRecyclerView.setLayoutManager(llm);
        // 如果Item够简单，高度是确定的，打开FixSize将提高性能
        mRecyclerView.setHasFixedSize(true);
        // 设置Item默认动画，加也行，不加也行
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setNestedScrollingEnabled(false);

        mCommonEmptyAdapter = new CommonEmptyAdapter(WorkPermitQueryListActivity.this, R.layout.layout_item_work_permit_query, mDataBeanList);
        mCommonEmptyAdapter.setItemDatasListener(new CommonEmptyAdapter.ItemDatasListener<String>() {
            @Override
            public void setItemDatas(CommonViewHolder holder, String dataBean, int position) {
                // TextView tv_danger_stop_day = holder.getView(R.id.tv_danger_stop_day);
                RotateTextView rtv_state = holder.getView(R.id.rtv_state);
                switch (position) {
                    case 0:
                        rtv_state.setText("有效");
                        break;
                    case 1:
                        rtv_state.setText("关闭确认");
                        break;
                    case 2:
                        rtv_state.setText("关闭");
                        break;
                    case 3:
                        rtv_state.setText("待确认");
                        break;
                    case 4:
                        rtv_state.setText("待审批");
                        break;
                    case 5:
                        rtv_state.setText("超期");
                        break;
                    case 6:
                        rtv_state.setText("取消");
                        break;
                    case 7:
                        rtv_state.setText("申请取消");
                        break;
                    case 8:
                        rtv_state.setText("取消申请");
                        break;
                }
            }
        });

        mCommonEmptyAdapter.setOnItemClickListener(new CommonEmptyAdapter.OnItemClickListener() {
            @Override
            public void setOnItemClickListener(View view, int position) {
//                Intent intent = new Intent(WorkPermitQueryListActivity.this, ApplyWorkPermitActivity.class);
//                Bundle bundle=new Bundle();
//                bundle.putInt("PERMIT_STATE",position);
//                intent.putExtras(bundle);
//                startActivity(intent);
                Intent intent = new Intent(WorkPermitQueryListActivity.this, WorkPermitDetailsActivity.class);
                Bundle bundle = new Bundle();
                switch (position) {
                    case 0:
                        bundle.putString("from", "effective");
                        break;
                    case 1:
                        bundle.putString("from", "closeConfirm");
                        break;
                    case 2:
                        bundle.putString("from", "close");
                        break;
                    case 3:
                        bundle.putString("from", "toBeConfirmed");
                        break;
                    case 4:
                        bundle.putString("from", "pendingApproval");
                        break;
                    case 5:
                        bundle.putString("from", "overdue");
                        break;
                    case 6:
                        bundle.putString("from", "cancel");
                        break;
                    case 7:
                        bundle.putString("from", "applyCancel");
                        break;
                    case 8:
                        bundle.putString("from", "cancelApply");
                        break;
                    default:
                        break;
                }
                intent.putExtras(bundle);
                startActivity(intent);
            }

            @Override
            public void setOnItemLongClickListener(View view, int position) {

            }
        });

        mRecyclerView.setAdapter(mCommonEmptyAdapter);
    }

    private void initData() {
        LicenseGetListInput licenseGetListInput = new LicenseGetListInput();
        LicenseGetListInput.ModelBean modelBean = new LicenseGetListInput.ModelBean();
        modelBean.setPageType("Apply");
        modelBean.setWorkRegion("");
        licenseGetListInput.setModel(modelBean);
        licenseGetListInput.setLimit(10);
        licenseGetListInput.setOffset(10 * (currentPage - 1));
        Call<String> call = HttpManager.getInstance().requestService().LICENSE_GETLIST(HttpManager.getInstance().requestBody(licenseGetListInput));
        HttpManager.getInstance().requestCallBack(call, new CommonCallBack<String>() {
            @Override
            public void requestSuccess(String result) {
                LicenseGetListOutput licenseGetListOutput = new Gson().fromJson(result, LicenseGetListOutput.class);
                if (licenseGetListOutput.getResult().equals("Success")) {
                    totalPage = licenseGetListOutput.getData().getRowCount();
                    List<LicenseGetListOutput.DataBean.PageDataBean> pageDataBeanList = licenseGetListOutput.getData().getPageData();
                    mDataBeanList.addAll(pageDataBeanList);
                    mCommonEmptyAdapter.notifyDataSetChanged();
                    mSmartRefresh.finishRefresh();
                    mSmartRefresh.finishLoadMore();
                } else {
                    ToastUtils.getInstance().showShortToastBottom(licenseGetListOutput.getMessage());
                }
            }

            @Override
            public void requestError(String errorMsg) {
                ToastUtils.getInstance().showShortToastBottom(errorMsg);
            }
        });
    }

}

